package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hdmall.business.common.util.RabbitUtil;
import com.hmall.mapper.MallPushMessageMapper;
import com.hmall.mapper.SendMsgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
 * @Author zhoutao
 * 消息推送服务
 */
@Service
public class MsgService {

    private MallPushMessageMapper pushMessageMapper;

    @Autowired
    public MsgService(MallPushMessageMapper pushMessageMapper) {
        this.pushMessageMapper = pushMessageMapper;
    }

    /**
     * @Date 2018/11/9 14:42
     * @Param [messageId, username, type, body, title, value]
     * @return com.hmall.common.ServerResponse
     * @Description // 只推送后台
     */
    public ServerResponse backDirectPush(Integer messageId, String username, String type, String body, String title, String value) {

        if (type == null) {
            return ServerResponse.createByErrorMessage("请输入正确的类型");
        }

        Integer noReadCount = pushMessageMapper.selectNoReadContByUsername(username);
        String pcMessageVoStr = "" + messageId + "," + username + "," + title + "," + body.replaceAll(",", "，")+ "," + noReadCount + "," + new Date().toLocaleString();

        if (username != null && !"".equals(username)) {
            //点对点 队列形式推送 type 队列名
            String pushWay = "direct";
            RabbitUtil.rabbitPushForDirect("backDirect", type, username,pcMessageVoStr, pushWay);
        }

        //保存信息记录
        return ServerResponse.createBySuccess();
    }
}
